package com.fingard.dsp.bank.directbank.bony02;

import com.caucho.hessian.client.HessianProxyFactory;
import com.client.service.DealService;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.net.ConnState;
import com.fingard.xml.XmlTextWriter;

import java.net.MalformedURLException;
import java.util.Date;


public class BONY02Base extends DirectBase {

    protected String transCode = "";//交易代码
    protected String seqNo = "";//交易流水号，查询类交易可以为空
    protected String busiDate = "";//业务日期，查询类交易可以为空
    protected String queryStartNo = "";//查询起始序号，翻页查询必输
    protected String queryNum = "";//每页查询笔数，翻页查询必输


    static {
        javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(
                new javax.net.ssl.HostnameVerifier() {
                    @Override
                    public boolean verify(String hostname, javax.net.ssl.SSLSession sslSession) {
                        return true;
                    }
                });
    }

    protected String getCharset() {
        return getBankConfig().getCharset("UTF-8");
    }

    protected String getQryTransID() {
        return Format.Time12Format.format(new Date());
    }


    protected String[] sendToBank(String p_reqStr) throws Exception {
        String[] retStr = new String[]{"", ""};
        retStr[0] = ConnState.FAIL;
        WriteBankLogLn("1.提交银行的地址：" + bankFront.serverURL);
        WriteBankLogStep2(p_reqStr);
        //发送给银行前置机
        HessianProxyFactory factory = new HessianProxyFactory();
        DealService basic;
        try {
            basic = (DealService) factory.create(DealService.class, bankFront.serverURL);
        } catch (MalformedURLException e) {
            retStr[0] = ConnState.FAIL;
            retStr[1] = e.toString();
            return retStr;
        }
        retStr[0] = "";
        try {
            retStr[1] = basic.transfer(p_reqStr);
        } catch (Exception e) {
            WriteBankLogLn("发送报文失败：" + e.toString());
        }
        WriteBankLogStep3(retStr[1]);

        return retStr;
    }

    /**
     * 写入通用头部节点
     *
     * @param xmlWriter 发银行报文的XML对象
     * @throws Exception
     */
    protected void writeCommonHeader(XmlTextWriter xmlWriter) throws Exception {
        xmlWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        xmlWriter.writeStartElement("request");
        xmlWriter.writeStartElement("header");
        xmlWriter.writeElementString("tradeCode", transCode);//交易代码
        Date dateNow = new Date();
        xmlWriter.writeElementString("reqTime", Format.DateTimeFormat.format(dateNow));//交易日期
        xmlWriter.writeElementString("serialId", seqNo);//交易流水号
        xmlWriter.writeEndElement();

    }

    /**
     * 判断响应码
     *
     * @param retCode
     * @return
     */
    protected static boolean isActiveRetCode(String retCode) {
        if (retCode != null) {
            if ("000000".equals(retCode)) {
                return true;
            }
        }
        return false;
    }

}
